package com.jlu.springsecurity.handler;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class MySuccessHander implements AuthenticationSuccessHandler {

  private String url;

  public MySuccessHander(String url){
    this.url = url;
  }

  @Override
  public void onAuthenticationSuccess(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Authentication authentication) throws IOException, ServletException {
    System.out.println(httpServletRequest.getRemoteAddr());
    User user = (User) authentication.getPrincipal();
    System.out.println("username:" + user.getUsername());
    System.out.println("password:" + user.getPassword());
    System.out.println("auth:" + user.getAuthorities());
    httpServletResponse.sendRedirect(url);
  }
}
